package com.it.od.new202211;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 字符串重新排序
 * 1.单词内排序：按照字典序排序
 * 2.单词间顺序调整
 * 1）统计单词出现次数降序
 * 2）次数相同按照单词长度升序排列
 * 3）次数和单词长度军相同，按照字典升序排列
 * 请输出处理后的字符串，每个单词以一个空格分隔
 * 输入
 * This is an apple
 * 输出
 * an is This aelpp
 */
public class Main3 {

    public static void main(String[] args) {
        //处理输入
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String[] str1 = str.split(" ");

        // 第一步，单词内部调整
        ArrayList<String> str_list = new ArrayList<String>();
        for (String s : str1) {
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            str_list.add(String.valueOf(chars));
            //System.out.println(s);
        }

        //第二步，单词间调整
        //先统计每个单词出现的次数
        HashMap<String, Integer> str_count = new HashMap<String, Integer>();

        for (int i = 0; i < str_list.size(); i++) {
            if (str_count.containsKey(str_list.get(i))) {
                str_count.put(str_list.get(i), str_count.get(str_list.get(i)) + 1);
            } else {
                str_count.put((str_list.get(i)), 1);
            }
        }

        // 按次数排序
        // 通过ArrayList构造函数把 str_count.entrySet()转换成list
        List<Map.Entry<String, Integer>> str_count_list = new ArrayList<Map.Entry<String, Integer>>(str_count.entrySet());
        // 通过比较器实现比较排序
        str_count_list.sort(new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
                //return ~mapping1.getValue().compareTo(mapping2.getValue());
                if (a.getValue() > b.getValue()) {
                    return -1;
                }
                if (a.getValue().equals(b.getValue())) {
                    if (a.getKey().length() > b.getKey().length()) {
                        return 1;
                    }
                    if (a.getKey().length() == b.getKey().length()) {
                        return a.getKey().compareTo(b.getKey());
                    }
                    return -1;
                }
                return 1;

            }
        });

        StringBuilder result = new StringBuilder();
        for (Map.Entry<String, Integer> stringIntegerEntry : str_count_list) {
            for (int j = 0; j < stringIntegerEntry.getValue(); j++) {
                result.append(stringIntegerEntry.getKey()).append(" ");
            }

        }
        System.out.println(result);
    }

}
